package org.phalanx.client;

import java.io.File;
import java.io.IOException;

import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.dialogs.PhalanxWindow;
import org.phalanx.client.net.PhalanxConnectionManager;
import org.phalanx.client.net.XMPPConnectionManager;
import org.phalanx.client.properties.ClientPrefXMLReader;
import org.phalanx.client.properties.ClientPreferences;
import org.phalanx.logger.SystemLogger;
import org.phalanx.map.MapTileSet;
import org.phalanx.map.PhalanxTileSetReader;

public class PhalanxClient {

	private static PhalanxClient currentClient;
	private SystemLogger logger;
	private ClientPreferences pref;
	private XMPPConnectionManager xmppConnectionMgr;
	private PhalanxConnectionManager nodeConnectionMgr;
	private static final Integer majorVersion = 0;
	private static final Integer minorVersion = 1;
	private static final Integer subVersion = 0;
	private MapTileSet defaultTileSet;
	
	public static void main(String[] args) {
		// configureArgs();
		currentClient = new PhalanxClient(/* Pass Args through when ready*/);
		currentClient.init();
		
	}
	
	PhalanxClient(){
	}
	
	public void init(){
		// Initialize the following
		// 1: Read in configuration file
		// 2: Configure client for passed in args (if applicable)
		// 3: Load up initial client screen
		
/*		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e){}*/

		readInPreferences();
		readInDefaultTileSet();
		clearCacheDirectories();
		createDialogs();
		xmppConnectionMgr = new XMPPConnectionManager();
		nodeConnectionMgr = new PhalanxConnectionManager();
		startupScreen();
	}
	
	public void readInPreferences(){
		pref = new ClientPreferences();
		new ClientPrefXMLReader(pref, "PhalanxProperties.xml").readXMLIntoPrefs(); 
	}
	
	public void createDialogs(){
		DialogManager.getInstance().setDialog(PhalanxWindow.NAME, new PhalanxWindow());
	}
	
	private void startupScreen(){
		DialogManager.getInstance().getDialog(PhalanxWindow.NAME).setVisible(true);
		
	}
	
	synchronized public static PhalanxClient getClientInstance(){
		return currentClient;
	}
	
	public void exit(int code){
		System.exit(code);
	}
	
	public ClientPreferences getPreferences(){
		return pref;
	}
	
	public SystemLogger getLogger(){
		return logger;
	}
	
	public XMPPConnectionManager getXMPPConnectionManager(){
		return xmppConnectionMgr;
	}
	
	public String formattedVersion(){
		return majorVersion + "." + minorVersion + "." + subVersion;
	}
	
	public boolean readInDefaultTileSet(){
		boolean isSuccess = false;
		
		PhalanxTileSetReader p = new PhalanxTileSetReader();
		try {
			String defaultTileSetFileName = pref.getProperties("PhalanxClient.PhalanxGame.Mapping.DefaultTileSet");
			p.setTerrainFileByName(defaultTileSetFileName);
			isSuccess = p.readInTerrain();
			this.defaultTileSet = p.getMapTileSet();
			defaultTileSet.setTileSetArchiveFile(defaultTileSetFileName);
			defaultTileSet.setCanonicalFilePath(defaultTileSetFileName);
			
		} catch (IOException ie){}
		
		return isSuccess;
	}
	
	public MapTileSet getDefaultTileSet(){
		return defaultTileSet;
	}
	
	private void clearCacheDirectories(){
		File dir = new File(this.pref.getProperties("PhalanxClient.PhalanxGame.Mapping.CacheDirectory", "./cache"));
		
		if (dir.list() != null){
			String[] cacheList = dir.list();
			
			for (int i = 0; i<cacheList.length; i++){
				File childCacheFile = new File(dir + "/" + cacheList[i]);
				
				childCacheFile.delete();
			}
		}
	}
	
	public PhalanxConnectionManager getPhalanxNodeConnectionManager() {
		return this.nodeConnectionMgr;
	}
}
